<?php
class EmployeesController extends AppController {

	var $name = 'Employees';
	var $uses = array('Employee'  );
	
	var $permissions = array(
		'add' => array('admin','manag'),
		'index' => array('admin','manag'),
		'view' => array('admin','manag'),
		'edit' => array('admin','manag'),
		'delete' => array('admin','manag')
	);
	public function beforeRender(){
		parent::beforeRender();
		
		$this->layout = 'cms';
	}
			
	function index() {
		$this->Employee->recursive = 0;
		$this->set('employees', $this->paginate());
	}
	
	function employee() {

	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid employee', true));
			$this->redirect(array('action' => 'index'));
		}
		
		
		$employee = $this->Employee->read(null, $id);
		$typeOfWorks = $this->Employee->TypeOfWork->find('list');
		$this->set(compact('employee', 'typeOfWorks'));
	}

	function add() {
		if (!empty($this->data)) {
			$this->data['User']['email'] = $this->data['Employee']['email'];
			$this->data['User']['password'] = $this->Auth->password($this->data['Employee']['password']);	
			$this->data['User']['status'] = 1;
			$this->data['User']['group'] = $this->data['Employee']['group'];
			if ($this->Employee->validates($this->data)) {
				$this->User->create();
				if ($this->User->save($this->data)) {
					$this->data['Employee']['user_id'] = $this->User->getLastInsertID();
					$this->data['Employee']['registration_date'] = date('Y-m-d H:i:s');				
					$this->Employee->create();
					if ($this->Employee->save($this->data)) {
						$this->Session->setFlash(__('The employee has been saved', true), 'flash_succes');
						$this->redirect(array('action' => 'index'));
					} else {
						$this->Session->setFlash(__('The employee could not be saved. Please, try again.', true), 'flash_error');
					}
								} else {
					$this->Session->setFlash(__('The employee could not be saved. Please, try again.', true), 'flash_error');
				}

			} else {
				$this->Session->setFlash(__('The employee could not be saved. Please, try again.', true), 'flash_error');	
				$this->redirect($this->referer());
			}					
		}
		$users = $this->Employee->User->find('list');
		$groups_ = Configure::read('Config.users_groups');
		$groups = array();
		foreach ($groups_ as $key => $value) {
			$groups[$key] = $value['name'];
		}		
		$typeOfWorks = $this->Employee->TypeOfWork->find('list');
		$this->set(compact('users','typeOfWorks','groups'));
	}

	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid employee', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Employee->save($this->data)){
				$this->Session->setFlash(__('The employee has been saved', true));
				$this->Employee->User->id = $this->data['Employee']['user_id'];
				if(!empty($this->data['User']['password_new'])){
					$this->data['User']['password'] = $this->Auth->password($this->data['User']['password_new']);
				}else{
					unset($this->data['User']['password_new']);
				}
				$this->Employee->User->save($this->data);
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The employee could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Employee->read(null, $id);
		}
		$users = $this->Employee->User->find('list');
		$groups_ = Configure::read('Config.users_groups');
		$groups = array();
		foreach ($groups_ as $key => $value) {
			$groups[$key] = $value['name'];
		}
		$typeOfWorks = $this->Employee->TypeOfWork->find('list');
		$this->set(compact('users', 'typeOfWorks','groups'));
	}

	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for employee', true));
			$this->redirect(array('action'=>'index'));
		}
		$user = $this->Employee->findById($id);
		if ($this->Employee->delete($id)) {			
			if ($this->Employee->User->delete($user['Employee']['user_id'])) {
				$this->Session->setFlash(__('Employee deleted', true));
				$this->redirect(array('action'=>'index'));
			}
		}
		$this->Session->setFlash(__('Employee was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}


}
?>